﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gfw.Models.Site;
using Gfw.Repositories.Site;
using Gfw.Web.Models.Site;

namespace Gfw.BLL.Basic.Site
{
    /// <summary>
    /// 公告管理操作
    /// </summary>
    public class AnnouncementLogic
    {
        #region 查询 add by lif

        //这里可以加入关键字
        /// <summary>
        /// 
        /// </summary>
        /// <param name="isFront">true:前台调用（会员）；false:后台调用（管理员）</param>
        /// <param name="query"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalSize"></param>
        /// <returns></returns>
        public static IList<AnnouncementModel> Query(bool isFront, string query, DateTime start, DateTime end, int pageIndex, int pageSize, out int totalSize)
        {
            AnnouncementRepository _anctRepository = new AnnouncementRepository();
            var _anctList = _anctRepository.Query(isFront, query, start, end, pageIndex, pageSize, out totalSize);

            List<AnnouncementModel> _anctModelList = new List<AnnouncementModel>();
            if (_anctList != null && _anctList.Count() > 0)
            {
                foreach (FctAnnouncement _item in _anctList)
                {
                    AnnouncementModel _anct = new AnnouncementModel()
                    {
                        ID = _item.Id,
                        AnnouncementDate = _item.AnnouncementDate,
                        AnnouncementPublisher = _item.AnnouncementPublisher,
                        AnnouncementTitle = _item.AnnouncementTitle,
                        AnnouncementKeyWord = _item.AnnouncementKeyWord,
                        AnnouncementContent = _item.AnnouncementContent,
                        IsShow = _item.IsShow == 1 ? true : false,
                    };

                    _anctModelList.Add(_anct);
                }
            }

            return _anctModelList;
        }

        #endregion

        /// <summary>
        /// 获取所有公告
        /// 这个方法暂时不用，用上面的 Query 方法
        /// </summary>
        /// <returns></returns>
        public static IList<AnnouncementModel> GetAllAnnouncements()
        {
            AnnouncementRepository _anctRepository = new AnnouncementRepository();
            IEnumerable<FctAnnouncement> _anctList = _anctRepository.GetAll().OrderByDescending(p => p.AnnouncementDate);

            List<AnnouncementModel> _anctModelList = new List<AnnouncementModel>();

            if (_anctList != null && _anctList.Count() > 0)
            {

                foreach (FctAnnouncement _item in _anctList)
                {
                    AnnouncementModel _anct = new AnnouncementModel()
                    {
                        ID = _item.Id,
                        AnnouncementDate = _item.AnnouncementDate,
                        AnnouncementPublisher = _item.AnnouncementPublisher,
                        AnnouncementTitle = _item.AnnouncementTitle,
                        AnnouncementKeyWord = _item.AnnouncementKeyWord,
                        AnnouncementContent = _item.AnnouncementContent,
                        IsShow = _item.IsShow == 1 ? true : false,
                    };

                    _anctModelList.Add(_anct);
                }


            }

            return _anctModelList;
        }



        /// <summary>
        /// 返回前十条最新公告
        /// </summary>
        /// <returns></returns>
        public static IList<AnnouncementModel> GetLatestAnnouncements()
        {
            AnnouncementRepository _anctRepository = new AnnouncementRepository();
            var _anctList = _anctRepository.GetLatestAnnouncements();

            List<AnnouncementModel> _anctModelList = new List<AnnouncementModel>();
            if (_anctList != null && _anctList.Count() > 0)
            {
                foreach (FctAnnouncement _item in _anctList)
                {
                    AnnouncementModel _anct = new AnnouncementModel()
                    {
                        ID = _item.Id,
                        AnnouncementDate = _item.AnnouncementDate,
                        AnnouncementPublisher = _item.AnnouncementPublisher,
                        AnnouncementTitle = _item.AnnouncementTitle,
                        AnnouncementKeyWord = _item.AnnouncementKeyWord,
                        AnnouncementContent = _item.AnnouncementContent,
                        IsShow = _item.IsShow == 1 ? true : false,
                    };

                    _anctModelList.Add(_anct);
                }
            }

            return _anctModelList;
        }

        /// <summary>
        /// 根据ID查询公告
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static AnnouncementModel GetAnnouncementByID(string id)
        {
            AnnouncementRepository _anctRepository = new AnnouncementRepository();
            FctAnnouncement _oneAnct = _anctRepository.Get(id);

            if (_oneAnct != null)
            {
                AnnouncementModel _anct = new AnnouncementModel()
                {
                    ID = _oneAnct.Id,
                    AnnouncementDate = _oneAnct.AnnouncementDate,
                    AnnouncementPublisher = _oneAnct.AnnouncementPublisher,
                    AnnouncementTitle = _oneAnct.AnnouncementTitle,
                    AnnouncementKeyWord = _oneAnct.AnnouncementKeyWord,
                    AnnouncementContent = _oneAnct.AnnouncementContent,
                    IsShow = _oneAnct.IsShow == 1 ? true : false,
                };

                return _anct;
            }

            return null;
        }

        /// <summary>
        /// 创建新公告
        /// </summary>
        /// <param name="model"></param>
        public static void CreateAnnouncement(AnnouncementModel model)
        {
            FctAnnouncement _anct = new FctAnnouncement()
            {
                Id = Guid.NewGuid().ToString(),
                AnnouncementDate = DateTime.Now,
                AnnouncementPublisher = model.AnnouncementPublisher,
                AnnouncementTitle = model.AnnouncementTitle,
                AnnouncementKeyWord = model.AnnouncementKeyWord,
                AnnouncementContent = model.AnnouncementContent,
                IsShow = model.IsShow ? 1 : 0,
            };

            AnnouncementRepository _repository = new AnnouncementRepository();
            _repository.Save(_anct);
        }

        /// <summary>
        /// 修改公告
        /// </summary>
        /// <param name="model"></param>
        public static void EditAnnouncement(AnnouncementModel model)
        {
            if (model == null)
                return;

            AnnouncementRepository _anctRepository = new AnnouncementRepository();
            FctAnnouncement _oneAnct = _anctRepository.Get(model.ID);

            if (_oneAnct != null)
            {
                _oneAnct.AnnouncementDate = DateTime.Now;
                _oneAnct.AnnouncementPublisher = model.AnnouncementPublisher;
                _oneAnct.AnnouncementTitle = model.AnnouncementTitle;
                _oneAnct.AnnouncementKeyWord = model.AnnouncementKeyWord;
                _oneAnct.AnnouncementContent = model.AnnouncementContent;
                _oneAnct.IsShow = model.IsShow ? 1 : 0;
            }

            _anctRepository.Update(_oneAnct);
        }

        /// <summary>
        /// 删除公告
        /// </summary>
        /// <param name="id"></param>
        public static void DeleteAnnouncement(string id)
        {
            AnnouncementRepository _repository = new AnnouncementRepository();
            _repository.Delete(id);
        }
    }
}
